﻿using System;
using System.Reflection;
using Glue.Internals.Nulls;

namespace Glue.Internals
{
    internal partial class AccessorFactory
    {
        private class PropertyAccessor : IObjectMemberAccessor
        {
            private readonly PropertyInfo property;

            public String Name { get; private set; }
            public bool CanGetValues { get; private set; }
            public bool CanSetValues { get; private set; }
            public Type PropertyType { get; private set; }

            public PropertyAccessor(PropertyInfo propertyInfo)
            {
                property = propertyInfo;
                Name = propertyInfo.Name;
                CanGetValues = property.GetGetMethod().AsMaybe().HasValue();
                CanSetValues = property.GetSetMethod().AsMaybe().HasValue();
                PropertyType = property.PropertyType;
            }

            public Maybe InvokeGet(object toInvokeOn)
            {
                return property.GetGetMethod().Invoke(toInvokeOn, null).AsMaybe();
            }

            public virtual void InvokeSet(object toInvokeOn, object toSet)
            {
                property.GetSetMethod().Invoke(toInvokeOn, new[] { toSet });
            }

            public bool CanMapFrom(object thisObject) { return true; }
        }
    }
}
